package com.lg.distributed.transaction.scheduled;

import com.lg.distributed.transaction.config.MyMQConfig;
import com.lg.distributed.transaction.domain.MqMessage;
import com.lg.distributed.transaction.service.MqMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author by Mr. Li 2020/12/11 11:06
 */
@Component
@Slf4j
public class MyRabbitScheduled {
    @Autowired
    private MqMessageService mqMessageService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //@Scheduled(cron = "* */3 * * * *")
    public void MessageRetry() {

        log.info("定时任务，查看mq中是否有消息没有发送成功，并且将该信息进行重发");
        List<MqMessage> mqMessageList = mqMessageService.selectByMessageStatusNo1();
        // 进行消息重试
        if (mqMessageList != null) {
            mqMessageList.forEach(mqMessage -> {
                //    public void convertAndSend(String exchange, String routingKey, Object object) throws AmqpException {
                rabbitTemplate.convertAndSend(MyMQConfig.DISTRIBUTED_TRANSACTION_EVENT_EXCHANGE, MyMQConfig.DISTRIBUTED_TRANSACTION_KEY,
                        mqMessage);
            });
        }
    }

}
